package com.yksj.healthtalk.comm;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.DoctorChatActivity;
import com.yksj.healthtalk.utils.SystemUtils;


/**
 * 系统医生聊天输入区域
 * @author zhao
 *
 */
public class DoctorChatInputFragment extends android.support.v4.app.Fragment implements OnClickListener{
	
	public EditText mEditText;
	DoctorInputControlListerner mControlListerner;
//	SeekBar mAgeBar;
//	public View mSeekbarSoftbord;
	View mDeletePanelV;
	View mInputPanelV;
	Button mDeletBtn;
//	public TableLayout mNumberSoftBord;
	
	final List<CheckBox> selectedList = new ArrayList<CheckBox>();
	public String mChoiceType = "2";//1单选 2多选
	
	public interface DoctorInputControlListerner{
		void onMessageSend(String content);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.chat_doctor_input_controller_layout,null);
		initView(view);
		return view;
	}
	
	
	@Override
	public void onAttach(Activity activity) {
		if(activity instanceof DoctorInputControlListerner){
			mControlListerner = (DoctorInputControlListerner)activity;
		}
		super.onAttach(activity);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.chat_send_btn:
			onSendMessage();
			break;
		case R.id.chat_switch:
			onChatSwitch();
			break;
		case R.id.chat_delete_btn1://全选
			DoctorChatActivity activity = (DoctorChatActivity)getActivity();
			activity.onSelectAll();
			break;
		case R.id.chat_delete_btn2://删除所有
			activity = (DoctorChatActivity)getActivity();
			activity.onDeletAll();
			break;
		case R.id.chat_delete_btn3://删除选中
			activity = (DoctorChatActivity)getActivity();
			activity.onDeletSelect();
			break;
		default://数字键盘选择
			if(v instanceof CheckBox){
				onNumberSoftClick((CheckBox)v);
			}
			break;
		}
	}
	
	/**
	 * 
	 * 数字键盘
	 * @param checkBox
	 * 
	 */
	private void onNumberSoftClick(CheckBox checkBox){
		if("1".equals(mChoiceType)){//单选
			if(!checkBox.isChecked()){//已经选中
				selectedList.clear();
				mEditText.setText(null);
			}else{
				for (int i = 0; i < selectedList.size(); i++) {
					selectedList.get(i).setChecked(false);
				}
				selectedList.clear();
				selectedList.add(checkBox);
				mEditText.setText(checkBox.getText());
			}  
		}else{//多选
			if(selectedList.contains(checkBox)){
				selectedList.remove(checkBox);
			}else{
				selectedList.add(checkBox);
			}
			String str = "";
			for (CheckBox checkBox2 : selectedList) {
				str+=checkBox2.getText()+",";
			}
			if(str.length()>0)str = str.substring(0, str.length()-1);
			mEditText.setText(str);
		}
	}
	
	/**
	 * 改变软键盘
	 * @param type
	 */
	public void onSoftBordChange(String type){
		hideSoftBord();
//		if("1".equals(type)){//数字键盘
//			hideSoftBord();
//			mNumberSoftBord.setVisibility(View.VISIBLE);
//			mSeekbarSoftbord.setVisibility(View.GONE);
//		}else if("2".equals(type)){//年龄键盘
//			hideSoftBord();
//			mNumberSoftBord.setVisibility(View.GONE);
//			mSeekbarSoftbord.setVisibility(View.VISIBLE);
//		}
	}
	
	public void hideSoftBord(){
		InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(mEditText.getWindowToken(),0);
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
//	private void hideAllSoftBord(){
//		mNumberSoftBord.setVisibility(View.GONE);
//		mSeekbarSoftbord.setVisibility(View.GONE);
//	}
	
	/**
	 * 编辑模式
	 * @param b
	 */
	public void onChangeEditorMode(boolean b){
		if(b){//编辑模式下
			hideSoftBord();
//			hideAllSoftBord();
			mDeletePanelV.setVisibility(View.VISIBLE);
			mInputPanelV.setVisibility(View.GONE);
		}else{//非编辑模式下
			mDeletePanelV.setVisibility(View.GONE);
			mInputPanelV.setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * 更新选中个数
	 * @param size
	 */
	public void onUpdateSelectedNumber(int size){
		mDeletBtn.setText("删除选中("+size+")");
	}
	
/*	private void showSoftBord(View view){
		InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
	}*/
	
	private void initView(View view){
		view.findViewById(R.id.chat_delete_btn1).setOnClickListener(this);
		view.findViewById(R.id.chat_delete_btn2).setOnClickListener(this);
		mDeletBtn = (Button)view.findViewById(R.id.chat_delete_btn3);
		mDeletBtn.setOnClickListener(this);
		mEditText = (EditText)view.findViewById(R.id.chat_edit);
		mDeletePanelV = view.findViewById(R.id.delete_panel);
		mInputPanelV = view.findViewById(R.id.input_layout);
		view.findViewById(R.id.chat_send_btn).setOnClickListener(this);
		view.findViewById(R.id.chat_switch).setOnClickListener(this);
//		mAgeBar = (SeekBar)view.findViewById(R.id.age_sekbar);
//		mNumberSoftBord = (TableLayout)view.findViewById(R.id.number_softbord);
//		mSeekbarSoftbord = view.findViewById(R.id.seekbar_softbord);

//		int childCount = mNumberSoftBord.getChildCount();
//		
//		for (int i = 0; i < childCount; i++) {
//			TableRow tableRow = (TableRow)mNumberSoftBord.getChildAt(i);
//			int count2 = tableRow.getChildCount();
//			for (int j = 0; j < count2; j++) {
//				tableRow.getChildAt(j).setOnClickListener(this);
//			}
//		}
//		
//		mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//			@Override
//			public void onFocusChange(View v, boolean hasFocus) {
//				hideAllSoftBord();
//			}
//		});
//		mEditText.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				hideAllSoftBord();
//			}
//		});
		
//		mAgeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
//			@Override
//			public void onProgressChanged(SeekBar seekBar, int progress,
//					boolean fromUser) {
//				mEditText.setText(String.valueOf(progress));
//			}
//
//			@Override
//			public void onStartTrackingTouch(SeekBar seekBar) {
//				
//			}
//
//			@Override
//			public void onStopTrackingTouch(SeekBar seekBar) {
//				
//			}
//		});
	}
	
	private void onChatSwitch(){
		hideSoftBord();
//		if(mNumberSoftBord.getVisibility() == View.GONE && mSeekbarSoftbord.getVisibility() == View.GONE){
//			hideSoftBord();
//			mNumberSoftBord.setVisibility(View.VISIBLE);
//			mSeekbarSoftbord.setVisibility(View.GONE);
//		}else if(mNumberSoftBord.getVisibility() == View.GONE){
//			hideSoftBord();
//			mNumberSoftBord.setVisibility(View.VISIBLE);
//			mSeekbarSoftbord.setVisibility(View.GONE);
//		}else if(mSeekbarSoftbord.getVisibility() == View.GONE){
//			hideSoftBord();
//			mNumberSoftBord.setVisibility(View.GONE);
//			mSeekbarSoftbord.setVisibility(View.VISIBLE);
//			mAgeBar.setProgress(0);
//		}
	}
	
	/**
	 * 显示数字键盘
	 */
//	public void onShowNumberBord(){
//		hideSoftBord();
//		mNumberSoftBord.setVisibility(View.VISIBLE);
//		mSeekbarSoftbord.setVisibility(View.GONE);
//	}
//	
	/**
	 * 年龄键盘
	 */
//	public void onShowAgeBord(){
//		hideSoftBord();
//		mNumberSoftBord.setVisibility(View.GONE);
//		mSeekbarSoftbord.setVisibility(View.VISIBLE);
//		mAgeBar.setProgress(0);
//	}
	
	private void onSendMessage(){
		SystemUtils.hideSoftBord(getActivity(),mEditText );
		String str = mEditText.getEditableText().toString();
		mEditText.setText(null);
		for (CheckBox box : selectedList) {
			box.setChecked(false);
		}
		selectedList.clear();
		if(mControlListerner != null)mControlListerner.onMessageSend(str);
	}
	
}
